<script setup>

import {getCurrentInstance, onMounted, reactive, ref, toRaw, watch} from "vue";
import restApi from "@/api/rest/restApi";

import {nanoid} from "nanoid";
import 'jsoneditor/dist/jsoneditor.min.css';
import ParamMapping from "@/views/restapi/ParamMapping.vue";

const props = defineProps(["entity"]);


watch(props, (newValue, oldValue) => {
	init();
});
const queryDefaultResponseWrapper = async () => {
	const defaultResponseWrapper = await restApi.queryDefaultResponseWrapper();
	if (!defaultResponseWrapper) {
		return
	}
	for (const responseWrapper of defaultResponseWrapper) {
		const id = nanoid(5);
		data.value.params.push({
			// description: responseWrapper.name,
			// key: responseWrapper.code,
			// mappingValue: null
			_key: id,
			fieldCode: responseWrapper.code,
			fieldName: responseWrapper.name,
			fieldType: responseWrapper.type,
			fieldDesc: responseWrapper.name,
			children: [],
			editorFlag: false,
			_showChildren: true,
			isArrayFlag: false,
			mappingValue: null
		})
	}
	//paramColumns.value = defaultResponseWrapper;
}


const data = ref({
	params: [],
	isArrayFlag: false,
	isViewBasicFieldFlag: false,
	isAddColumnFlag: false,
	isUpdateColumnFlag: false,
	isActionColumnFlag: true,
	isRemoveColumnFlag: false,
	isAddChildrenColumnFlag: false,
	isMappingColumnFlag: true,
	isViewArrayFlag: false,
	basicTypeFlag: false,
	basicFieldType: false
});

const rightData = ref({
	params: [],
	isArrayFlag: false,
	isViewBasicFieldFlag: false,
	isViewArrayFlag: false,
	isMappingColumnFlag: true,
	isActionColumnFlag: true,
	basicTypeFlag: false,
	basicFieldType: false
});

const init = () => {
	if (!props.entity.sourceFieldDefine) {
		props.entity.sourceFieldDefine = data.value;
	}
	console.log(props.entity)

	if (props.entity.sourceFieldDefine.params.length <= 0) {
		props.entity.sourceFieldDefine.params = data.value.params;
	}

	if (!props.entity.targetFieldDefine) {
		props.entity.targetFieldDefine = rightData.value;
	}
}

init();

onMounted(async () => {
	await queryDefaultResponseWrapper();
	init();
})

</script>

<template>
	<a-card>
		<a-row justify="start">
			<a-col :span="6">
				<a-checkbox v-model:checked="props.entity.resultWrapperFlag">返回值是否包裹固定结构</a-checkbox>
			</a-col>
		</a-row>
	</a-card>
	<a-row justify="start" v-if="props.entity.resultWrapperFlag">
		<a-col :span="24">
			<ParamMapping :leftData="props.entity.sourceFieldDefine" :rightData="props.entity.targetFieldDefine">
			</ParamMapping>
		</a-col>
	</a-row>
</template>

<style scoped></style>
